Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Zipper.map #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

tad-lispy
Copy link

@tad-lispy tad-lispy commented Jun 16, 2019

Allows to map all labels in a Zipper and preserve focus. See doc examples in src/Tree/Zipper.elm.

Also I've upgraded elm dependency in package.json to latest elm-0.19.0-no-deps. Without it npm install would fail for me.

Use case

I was looking for a way to display a Rose Tree Zipper as Html msg and highlight the focused sub-tree.

Currently there is Tree.restructure (https://package.elm-lang.org/packages/zwilias/elm-rosetree/latest/Tree#restructure), but I can't think of a way to pass the information about what is currently focused. I thought about mapping the tree into another tree that contains highlighted flag, and then updating the focused label to set highlighted to True, but again I don't know how to preserve the focus.

With Zipper.map it is possible as follows:

type alias Notebook =
    Zipper String


type alias Visual =
    { highlighted : Bool
    , text : String
    }


display : Notebook -> Element msg
display notebook =
    notebook
        |> Zipper.map (Visual False)
        |> Zipper.mapLabel (\visual -> { visual | highlighted = True })
        |> Zipper.toTree
        |> Tree.restructure identity displayItem


displayItem : Visual -> List (Element msg) -> Element msg
displayItem item children =
    Element.column
        [ Border.width 1
        , Element.padding 10
        , Element.spacing 5
        , Background.color
            (if item.highlighted then
                Element.rgb 1 1 0.6

             else
                Element.rgb 1 1 1
            )
        ]
        [ Element.text item.text
        , Element.column [ Element.spacing 5 ] children
        ]

I'm using mdgriffith/elm-ui in the example, but it's trivial to translate it to elm/html

It's the latest on npmjs.org and without it the `npm install` command
fails with the error:

  ReferenceError: primordials is not defined
@zwilias zwilias self-assigned this Jun 17, 2019
@tad-lispy
Copy link
Author

Hey! Any thoughts on this? It's OK if you are busy.

@nahiyan
Copy link

nahiyan commented Mar 21, 2021

It's sad that such amazing projects are abandoned like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants